
\begin{algorithm}[htbp]
	\caption{ 2VLL algorithm (Part I)}
	\label{alg:2vll}
	\small
	\xxx{def} \textbf{Begin-Update(Txn T)}
	
	\begin{algorithmic}[1]
		\STATE <begin critical section1>
		\STATE T.Type = Free
		\FOR{each key in T.ReadSet}
			\STATE key.Cs++
			\IF{key.Cx > 0}
				\STATE T.Type = Blocked
			\ENDIF
		\ENDFOR
		\FOR{each key in T.WriteSet}
			\STATE key.Cx++
			\IF{key.Cx > 1 OR key.Cs > 0}
				\STATE T.Type = Blocked
			\ENDIF
		\ENDFOR
		\STATE UpdateQueue.push\_back(T)	
		\STATE <end critical section1>
	\end{algorithmic}

	\xxx{def} \textbf{Commit-Update(Txn T)}
	
	\begin{algorithmic}[1]
	\STATE <begin critical section2>	
	\FOR{each key in T.ReadSet}
		\STATE key.Cs--;
	\ENDFOR
	\STATE OrderQueue.push\_back(T)
	\STATE UpdateQueue.Remove(T)	
    \STATE // write command log	
	\STATE <end critical section2>
	\end{algorithmic}	

	\xxx{def} \textbf{Update-deamon()}

	\begin{algorithmic}[1]
		\WHILE{true}
			\IF{UpdateQueue.front().Type == Blocked}
				\STATE Txn T = UpdateQueue.front();
				\STATE T.Type = Free
				\STATE Update-V2(T)
				\STATE Commit-Update(T)
			\ENDIF
		\ENDWHILE
	\end{algorithmic}

	\xxx{def} \textbf{Ordering-Txn(Txn T)}

	\begin{algorithmic}[1]
\WHILE{true}
        \IF{T is update}
		\IF{UpdateQueue is not full}			
			\STATE Begin-Update(T)
			\IF{T.Type = Free}
				\STATE Update-V2(T)
				\STATE Commit-Update(T)
			\ENDIF
		\ENDIF
        \ENDIF
        \IF{T is query}
            \STATE OrderQueue.push\_back(T)
        \ENDIF
        \ENDWHILE
	\end{algorithmic}
\end{algorithm}

\begin{algorithm}[htbp]
	\caption{ 2VLL algorithm (part II)}
	\label{alg:2vll-2}
	\small
	\xxx{def} \textbf{Single-Writer-Multiple-Reader()}
	
	\begin{algorithmic}[1]
		\WHILE{true}
					
		\STATE Txn T = CommitQueue.front()
\IF{T is Query}
\STATE OrderQueue.Remove(T)
        \STATE new query thread(Query-Txn)
        \ENDIF
		\ENDWHILE
        \IF{T is Update}
        \IF{query = 0}
			\FOR{each key in T.WriteSet}
				\STATE key.V1 = key.V2     //这里相当于只是添加了指针操作
                \STATE key.Cx--;
			\ENDFOR
			\STATE OrderQueue.Remove(T)
			\STATE T.Type = Committed
        \ENDIF			
		\ENDIF      
	\end{algorithmic}

	\xxx{def} \textbf{Query-Txn(Txn T)}
	
	\begin{algorithmic}[1]
			\STATE query++			
			\STATE Query-V1(T)
			\STATE query--
	\end{algorithmic}
\end{algorithm} 




